local SkillPanelView = require("app.battleV3.ui.views.subviews.SkillPanelView")
local BuffView 		 = require("app.battleV3.ui.views.subviews.BuffView")
local StatusView 	 = require("app.battleV3.ui.views.subviews.StatusView")
local SkillEvent     = require("app.battleV3.ui.objs.SkillEvent")


local BottomView = MClass("BottomView")

function BottomView:ctor(battleType)
	--self.root 默认为根节点
    self.battleType = battleType
    self.skillPanelView = SkillPanelView.new(self:findChild("Panel_skill"))
    self.buffView 		= BuffView.new(self:findChild("Panel_buff"))
    self.statusView 	= StatusView.new(self:findChild("Panel_bar"))
    self.btn_tips 		= self:findChild("btn_tips")
    self.chatViewPos = self:findChild("Node_chat")
    self.skillEvent = SkillEvent.new(MainBattleUICache:find(Res.Node_skillevent))

    self:addChild(self.skillEvent.root)
    local contentSize = self:getContentSize()
    self.skillEvent:setPosition(cc.p(contentSize.width/2,contentSize.height))
    self.skillEvent:bindEvent("mainui")
    self.chatPosY = self.chatViewPos:getPositionY()
    self.buffPosY = self.buffView:getPositionY()
    self:init()
end

function BottomView:init()
    -- body
    self.bShowChat = UD.setting:isShowBattleChat()
    
    local handle = EM:on("onSettingShowBattleChat", function(name, isShow)
        self:showChat(isShow)
    end)
    BattleUIEvent:addEmHandle(handle)

    local function touchEvent_Chat(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.QC_SHOW)
        end
    end
    self.btn_tips:addTouchEventListener(touchEvent_Chat)
    self.btn_tips:setVisible(false)
    --BattleUIEvent:addEventListener(BattleUIEvent.TYPE.QC_SHOW_BTN,handler(self,BottomView.showChatBtn))

    self:performWithDelay(function()
        self:showChat(self.bShowChat)
    end,0.5)

    --self:showChat(self.bShowChat)
    --add onEnter onExit
    local function onNodeEvent(event)
        if "enter" == event then
            self:onEnter()
        elseif "exit" == event then
            self:onExit()
        end
    end
    self:registerScriptHandler(onNodeEvent) 
end

function BottomView:onEnter()
end

function BottomView:onExit()
    self.buffView:setPositionY(self.buffPosY)
    self.buffView:reset()
    self.chatViewPos:setPositionY(self.chatPosY)
    self:unregisterScriptHandler()
    if self.skillEvent then
        self.skillEvent:removeFromParent()
    end
    self.statusView:clearHideEffect()
end

function BottomView:showChatBtn(bShow)
    self.btn_tips:setVisible(bShow)
end

function BottomView:showChat(isShow)
    self.chatType = Enums.IMSimpleShowType.all
    if self.bMelee then
        self.chatType = Enums.IMSimpleShowType.btn
        if isShow then
            app:sendMsg("IMSimpleController", "showView", self.chatType, self.chatPosY, true, nil, self.battleType)
        else
            app:sendMsg("IMSimpleController", "hideView")
        end
        return
    end
    self.bShowChat = isShow
    if not isShow then
        app:sendMsg("IMSimpleController", "hideView")
        self.skillEvent:setPositionY(self.buffPosY+50-82)
        self.buffView:setPositionY(self.buffPosY-82)
    else
        app:sendMsg("IMSimpleController", "showView", self.chatType, self.chatPosY, true, nil, self.battleType)
        self.skillEvent:setPositionY(self.buffPosY+50)
        self.buffView:setPositionY(self.buffPosY)
        --BattleUIEvent:addObjToFrameUpdate(self,BottomView.tick)
    end   
end

function BottomView:setMeleeLayout()
    self.bMelee = true
    self.skillEvent:setPositionY(self.buffPosY+50)
    self.buffView:setPositionY(self.buffPosY-20)
    self:showChat(self.bShowChat)
end


return BottomView